/*
 * Copyright (C), 2002-2018, Caljm有限公司
 * FileName: Producer
 * Author:   Byron Wang
 * Date:     2018/12/15 下午9:29
 * Description: //模块目的、功能描述
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */

package com.cpcs.mq.quickstart;

import com.cpcs.Constants;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 功能描述：消息的生产者
 *
 * @author Byron Wang
 * @since 2018/12/15
 */
public class Producer {

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "test_start_exchange";
        String queueName = "test_start_queue";
        String routingKey = "test.save";

        channel.exchangeDeclare(exchangeName, "direct", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        String msg = "RabbitMQ start Message";
        channel.basicPublish(exchangeName, routingKey, false, null, msg.getBytes());

        channel.close();
        connection.close();
    }
}
